package com.test;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.westcom.basicstruct.CardListStruct;
import com.westcom.basicstruct.CategoryListStruct;
import com.westcom.basicstruct.CategoryListStruct.Cdata.Cuncategorized;
import com.westcom.basicstruct.CategoryStatisticStruct;
import com.westcom.basicstruct.CategoryStatisticStruct.Cdata.Ccategories;
import com.westcom.basicstruct.CreateCardStruct;
import com.westcom.basicstruct.CreateCategoryStruct;
import com.westcom.basicstruct.CreateSheetStruct;
import com.westcom.basicstruct.GetCardListStruct;
import com.westcom.basicstruct.LogInStruct;
import com.westcom.basicstruct.SheetListStruct;
import com.westcom.basicstruct.SheetListStruct.Cdata.CSheets;
import com.westcom.basicstruct.SheetListStruct.Cdata.Cmembers;
import com.westcom.basicstruct.UploadStruct;

public class AnalyzeJson {
	public LogInStruct AnalyzeLogin(JSONObject jsonObject) throws JSONException
	{
		LogInStruct login = new LogInStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		login.result.message = result.optString("message");
		login.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");
			login.data.cardSize = data.optInt("cardSize");
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			login.data = null;
			e.printStackTrace();
			return login;
		}
		
		try {
			JSONObject confirmInvitationResult = data.getJSONObject("confirmInvitationResult");
			login.data.confirmInvitationResult.message = confirmInvitationResult.optString("message");
			login.data.confirmInvitationResult.statusCode = confirmInvitationResult.optString("statusCode");
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			login.data.confirmInvitationResult = null;
			e.printStackTrace();
		}
		JSONObject course = data.getJSONObject("course");
		login.data.course.capacity = course.optInt("capacity");
		login.data.course.description = course.optString("description");
		login.data.course.maxSheetCards = course.optInt("capacity");
		login.data.course.maxSheetMembers = course.optInt("maxSheetMembers");
		login.data.course.maxSheets = course.optInt("maxSheets");
		login.data.course.name = course.optString("name");
		
		login.data.currentCapacity = data.optInt("currentCapacity");
		login.data.fbId = data.optString("fbId");
		login.data.isPwdSetted = data.optString("isPwdSetted");
		login.data.locale = data.optString("locale");
		login.data.sessionId = data.optString("sessionId");
		login.data.tokenId = data.optString("tokenId");
		login.data.userConfig = data.optString("userConfig");
		login.data.userId = data.optInt("userId");
		login.data.userName = data.optString("userName");
		
		return login;
	}
	public UploadStruct AnalyzeUpload(JSONObject jsonObject) throws JSONException
	{
		UploadStruct upload = new UploadStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		upload.result.message = result.optString("message");
		upload.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");
			upload.data.currentCapacity = data.optInt("currentCapacity");
			upload.data.resourceId = data.optInt("resourceId");
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			upload.data = null;
			e.printStackTrace();
			return upload;
		}
		return upload;
	}
	
	public CreateSheetStruct AnalyzeCreateSheet(JSONObject jsonObject) throws JSONException
	{
		CreateSheetStruct createsheet = new CreateSheetStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		createsheet.result.message = result.optString("message");
		createsheet.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");	
			createsheet.data.sheetId = data.optInt("sheetId");	
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			createsheet.data = null;
			e.printStackTrace();
			return createsheet;
		}
		return createsheet;
	}
	
	public CreateCategoryStruct AnalyzeCreateCategory(JSONObject jsonObject) throws JSONException
	{
		CreateCategoryStruct createcategory = new CreateCategoryStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		createcategory.result.message = result.optString("message");
		createcategory.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");	
			createcategory.data.categoryId = data.optInt("categoryId");

			
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			createcategory.data = null;
			e.printStackTrace();
			return createcategory;
		}
		return createcategory;
	}
	
	
	
	public CreateCardStruct AnalyzeCreateCard(JSONObject jsonObject) throws JSONException
	{
		CreateCardStruct createcard = new CreateCardStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		createcard.result.message = result.optString("message");
		createcard.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");
			
			createcard.data.bad = data.optInt("bad");
			createcard.data.cardId = data.optInt("cardId");
			createcard.data.commentCount = result.optInt("commentCount");
			createcard.data.contents = result.optString("contents");
			createcard.data.createDate = result.optString("createDate");
			createcard.data.createUserId = result.optInt("createUserId");
			createcard.data.createUserName = result.optString("createUserName");
			createcard.data.good = data.optInt("good");
			createcard.data.resourceIds = result.optString("resourceIds");
			createcard.data.updateDate = result.optString("updateDate");
			createcard.data.updateUserId = data.optInt("updateUserId");
			createcard.data.updateUserName = result.optString("updateUserName");
			createcard.data.valuationStatus = data.optInt("valuationStatus");
			
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			createcard.data = null;
			e.printStackTrace();
			return createcard;
		}
		return createcard;
	}
	///have problem
	public SheetListStruct AnalyzeSheetList(JSONObject jsonObject) throws JSONException
	{
		SheetListStruct sheetlist = new SheetListStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		sheetlist.result.message = result.optString("message");
		sheetlist.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");
			JSONArray sheetarray = data.getJSONArray("sheets");
			for(int i=0;i<sheetarray.length();i++){
				CSheets sheet = sheetlist.data.new CSheets();
				JSONObject jsonsheet = sheetarray.getJSONObject(i);
				sheet.confirmFlg = jsonsheet.optString("confirmFlg");
				JSONArray membersarray = jsonsheet.getJSONArray("members");
				for(int j=0;j < membersarray.length();j++){
					Cmembers members = sheetlist.data.new Cmembers();
					JSONObject jsonmember = membersarray.getJSONObject(j);
					members.invitorUserName = jsonmember.optString("invitorUserName");
					members.mailAddress = jsonmember.optString("mailAddress");
					members.memberStatus = jsonmember.optString("memberStatus");
					members.ownFlg = jsonmember.optString("ownFlg");
					members.userId = jsonmember.optInt("userId");
					members.userName = jsonmember.optString("userName");
					sheet.members.add(members);
				}
				sheet.newCardCount = jsonsheet.optInt("newCardCount");
				sheet.ownFlg = jsonsheet.optString("ownFlg");
				sheet.sampleSheetFlg = jsonsheet.optString("sampleSheetFlg");
				sheet.shareFlg = jsonsheet.optString("shareFlg");
				sheet.sheetDescription = jsonsheet.optString("sheetDescription");
				sheet.sheetId = jsonsheet.optInt("sheetId");
				sheet.sheetName = jsonsheet.optString("sheetName");
				sheet.totalCard = jsonsheet.optInt("totalCard");
				sheet.totalCategory = jsonsheet.optInt("totalCategory");
				sheet.upCardCount = jsonsheet.optInt("upCardCount");
				sheet.verNo = jsonsheet.optInt("verNo");
				
				sheetlist.data.Sheets.add(sheet);	
			}
			
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			sheetlist.data = null;
			e.printStackTrace();
			return sheetlist;
		}
		return sheetlist;
	}
	public CategoryStatisticStruct AnalyzeCategoryStatistic(JSONObject jsonObject) throws JSONException
	{
		CategoryStatisticStruct categorystatistic = new CategoryStatisticStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		categorystatistic.result.message = result.optString("message");
		categorystatistic.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");
			JSONArray sheetarray = data.getJSONArray("categories");
			for(int i=0;i<sheetarray.length();i++){
				Ccategories categories = categorystatistic.data.new Ccategories();
				JSONObject jsonsheet = sheetarray.getJSONObject(i);
				categories.categoryId = jsonsheet.optInt("categoryId");
				categories.newCardCount = jsonsheet.optInt("newCardCount");
				categories.totalCard = jsonsheet.optInt("totalCard");
				categories.upCardCount = jsonsheet.optInt("upCardCount");
				
				categorystatistic.data.categories.add(categories);	
			}			
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			categorystatistic.data = null;
			e.printStackTrace();
			return categorystatistic;
		}
		return categorystatistic;
	}
	
	public CategoryListStruct AnalyzeCategoryList(JSONObject jsonObject) throws JSONException
	{
		CategoryListStruct categorylist = new CategoryListStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		categorylist.result.message = result.optString("message");
		categorylist.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");
			JSONArray uncategorizedarray = data.getJSONArray("categorized0");
			for(int i=0; i<uncategorizedarray.length(); i++){
				Cuncategorized categories = categorylist.data.new Cuncategorized();
				JSONObject jsonsheet = uncategorizedarray.getJSONObject(i);
				categories.categoryId = jsonsheet.optInt("categoryId");
				categories.categoryName = jsonsheet.optString("categoryName");
				categories.colorIdx = jsonsheet.optInt("colorIdx");
				categories.totalCard = jsonsheet.optInt("totalCard");
				categories.verNo = jsonsheet.optInt("verNo");
				
				categorylist.data.categorized0.add(categories);	
			}			
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			categorylist.data = null;
			e.printStackTrace();
			return categorylist;
		}
		return categorylist;
	}
	
	public CardListStruct AnalyzeCardList(GetCardListStruct[] categoriesmess, JSONObject jsonObject) throws JSONException
	{
		CardListStruct cardlist = new CardListStruct();
		
		
		JSONObject result = jsonObject.getJSONObject("result");
		cardlist.result.message = result.optString("message");
		cardlist.result.statusCode = result.optString("statusCode");
		
		JSONObject data;
		try {
			data = jsonObject.getJSONObject("data");
			JSONObject categories = data.getJSONObject("categories");
			for(int i=0;i<categoriesmess.length;i++){
				String categoruId = String.valueOf((categoriesmess[i].categoryId));
				JSONArray cardarray = categories.getJSONArray(categoruId);
				List<com.westcom.basicstruct.CardListStruct.Ccategories> categorie = new ArrayList<com.westcom.basicstruct.CardListStruct.Ccategories> ();
				for(int j=0;j<cardarray.length();j++){
					com.westcom.basicstruct.CardListStruct.Ccategories onecategory = cardlist.new Ccategories();
					JSONObject jsoncard = cardarray.getJSONObject(j);
					onecategory.bad = jsoncard.optInt("bad");
					onecategory.beginning = jsoncard.optString("beginning");
					onecategory.cardId = jsoncard.optInt("cardId");
					onecategory.commentCount = jsoncard.optInt("commentCount");
					onecategory.fileName = jsoncard.optString("fileName");
					onecategory.good = jsoncard.optInt("good");
					onecategory.readStatus = jsoncard.optString("readStatus");
					onecategory.resourceId = jsoncard.optInt("resourceId");
					onecategory.resourceType = jsoncard.optString("resourceType");
					onecategory.userId = jsoncard.optInt("userId");
					onecategory.userName = jsoncard.optString("userName");
					onecategory.valuationUpdateAt = jsoncard.optString("valuationUpdateAt");
					onecategory.verNo = jsoncard.optInt("verNo");
					categorie.add(onecategory);
				}
				cardlist.data.map.put(categoruId, categorie);	
			}			
		}
		catch(JSONException e) {
			// TODO Auto-generated catch block
			cardlist.data = null;
			e.printStackTrace();
			return cardlist;
		}
		return cardlist;
	}
}
